package com.quectel.core.module.securitypatrol.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.securitypatrol.dao.SecurityPatrolTaskRouteDao;
import com.quectel.core.module.securitypatrol.dto.SecurityPatrolTaskRouteDto;
import com.quectel.core.module.securitypatrol.entity.SecurityPatrolTaskRouteEntity;
import com.quectel.core.module.securitypatrol.service.SecurityPatrolTaskRouteService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2022/06/08 12:22
 */
@DubboService
public class SecurityPatrolTaskRouteServiceImpl implements SecurityPatrolTaskRouteService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityPatrolTaskRouteServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SECURITY_PATROL_TASK_ROUTE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SECURITY_PATROL_TASK_ROUTE_CACHE_BY_ID:%d", id);

    @Autowired
    private SecurityPatrolTaskRouteDao securityPatrolTaskRouteDao;


    @Override
    public SecurityPatrolTaskRouteDto selectOne(Long taskId, Long deviceId) {
        SecurityPatrolTaskRouteEntity entity = securityPatrolTaskRouteDao.selectOne(
                new LambdaQueryWrapper<SecurityPatrolTaskRouteEntity>()
                        .eq(SecurityPatrolTaskRouteEntity::getTaskId, taskId)
                        .eq(SecurityPatrolTaskRouteEntity::getDeviceId, deviceId)
        );
        return paddingField(CopyUtils.copyObj(entity, SecurityPatrolTaskRouteDto.class));
    }

    @Override
    public SecurityPatrolTaskRouteDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SECURITY_PATROL_TASK_ROUTE_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SecurityPatrolTaskRouteDto selectById(Long id) {
        SecurityPatrolTaskRouteEntity entity = securityPatrolTaskRouteDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SecurityPatrolTaskRouteDto.class));
    }

    @Override
    public List<SecurityPatrolTaskRouteDto> queryList(Map<String, Object> params) {
        List<SecurityPatrolTaskRouteEntity> list = securityPatrolTaskRouteDao.queryList(params);
        List<SecurityPatrolTaskRouteDto> result = CopyUtils.copyList(list, SecurityPatrolTaskRouteDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private SecurityPatrolTaskRouteDto paddingField(SecurityPatrolTaskRouteDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return securityPatrolTaskRouteDao.queryTotal(params);
    }

    @Override
    public Long save(SecurityPatrolTaskRouteDto dto) {
        SecurityPatrolTaskRouteEntity entity = CopyUtils.copyObj(dto, SecurityPatrolTaskRouteEntity.class);
        entity.setId(Snowflake.nextId());
        securityPatrolTaskRouteDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SecurityPatrolTaskRouteDto dto) {
        SecurityPatrolTaskRouteEntity entity = CopyUtils.copyObj(dto, SecurityPatrolTaskRouteEntity.class);
        securityPatrolTaskRouteDao.updateById(entity);
        CacheKit.invalidRedisCache(SECURITY_PATROL_TASK_ROUTE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(SecurityPatrolTaskRouteDto dto) {
        SecurityPatrolTaskRouteEntity entity = CopyUtils.copyObj(dto, SecurityPatrolTaskRouteEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, securityPatrolTaskRouteDao);

        CacheKit.invalidRedisCache(SECURITY_PATROL_TASK_ROUTE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        securityPatrolTaskRouteDao.deleteById(id);

        CacheKit.invalidRedisCache(SECURITY_PATROL_TASK_ROUTE_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
